package com.tzh.plugin.base;

import com.tzh.plugin.utils.PluginAnnotationUtils;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.util.List;

public class SwaggerPlugin extends PluginAdapter {


    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    /**
     * 拦截 普通字段
     *
     * @param topLevelClass
     * @param introspectedTable
     * @return
     */
    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
                                                 IntrospectedTable introspectedTable) {
        List<Method> methods = topLevelClass.getMethods();
        methods.stream().forEach(method -> {
            method.getJavaDocLines().clear();
        });
        List<Field> fields = topLevelClass.getFields();
        fields.stream().forEach(field -> {
            field.getJavaDocLines().clear();
        });

        PluginAnnotationUtils.addDataAnnotation(topLevelClass, introspectedTable);
        return true;
    }

    /**
     * 拦截 主键
     *
     * @param topLevelClass
     * @param introspectedTable
     * @return
     */
    @Override
    public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,
                                                 IntrospectedTable introspectedTable) {
        PluginAnnotationUtils.addDataAnnotation(topLevelClass, introspectedTable);
        return true;
    }

    /**
     * 拦截 blob 类型字段
     *
     * @param topLevelClass
     * @param introspectedTable
     * @return
     */
    @Override
    public boolean modelRecordWithBLOBsClassGenerated(
            TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        PluginAnnotationUtils.addDataAnnotation(topLevelClass, introspectedTable);
        return true;
    }

    @Override
    public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        PluginAnnotationUtils.comment(field, introspectedTable, introspectedColumn);
        return true;
    }


    @Override
    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        List<Method> methods = topLevelClass.getMethods();
        methods.stream().forEach(method -> {
            method.getJavaDocLines().clear();
        });

        List<Field> fields = topLevelClass.getFields();
        fields.stream().forEach(field -> {
            field.getJavaDocLines().clear();
        });

        List<InnerClass> clses = topLevelClass.getInnerClasses();
        clses.stream().forEach(cls -> {
            cls.getJavaDocLines().clear();
        });
        return true;
    }


}
